package com.qianji.petmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.Query;
import com.qianji.petmall.product.dao.AttrAttrgroupRelationDao;
import com.qianji.petmall.product.dao.AttrDao;
import com.qianji.petmall.product.dao.AttrGroupDao;
import com.qianji.petmall.product.entity.AttrAttrgroupRelationEntity;
import com.qianji.petmall.product.entity.AttrEntity;
import com.qianji.petmall.product.entity.AttrGroupEntity;
import com.qianji.petmall.product.service.AttrAttrgroupRelationService;
import com.qianji.petmall.product.vo.AttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrGroupDao attrGroupDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void deleteAll(AttrGroupVo[] attrGroupVo) {
        List<AttrAttrgroupRelationEntity> collect = Arrays.stream(attrGroupVo).map((attr) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attr, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        relationDao.deleteBYIds(collect);

    }

    @Override
    public List<AttrEntity> getNoatter(Long attrgroupId, Map<String, Object> params) {
        //查询当前所属分类中的所有属性
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
        //查询其它所属分类下没有引用的属性
        List<AttrEntity> list = attrDao.selectByThis();
        String key = (String) params.get("key");
        if(StringUtils.isNotBlank(key)){
            list = attrDao.selectList(new QueryWrapper<AttrEntity>().eq("attr_id", key).or().like("attr_name", key));
        }
        return list;
    }

    @Override
    public void addRelation(List<AttrGroupVo> list) {
        List<AttrAttrgroupRelationEntity> collect = list.stream().map(item -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

}